﻿using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;
using ALC.Core.Command;
using ALC.Core.Constants;
using ALC.Core.Enums;
using ALC.Core.ViewModels;
using ALC.Core.ViewModels.Message;
using WPFCommon.Commands;
using WPFCommon.Helpers;
using WPFCommon.ViewModels.Base;
using WPFCommon.ViewModels.Logger;
using WPFCommon.ViewModels.Message;

namespace ALC.Core
{
    public class MainWindowViewModel : ViewModelBase
    {
        public static MainWindowViewModel Instance { get; } = new MainWindowViewModel()
        {
            CurrentApplicationPage = ApplicationPage.Home
        };

        public ApplicationPage CurrentApplicationPage { get; set; }

        #region Commands

        public ICommand SwitchHomePageCommand { get; set; }
        public ICommand SwitchCameraPageCommand { get; set; }
        public ICommand SwitchLaserPageCommand { get; set; }
        public ICommand SwitchServerPageCommand { get; set; }

        public ICommand SwitchSettingsPageCommand { get; }

        public ICommand SwitchTablePageCommand { get; }
        public ICommand SwitchChartPageCommand { get; }

        public ICommand SwitchLoginPageCommand { get; }

        public ICommand OpenHelpDocsCommand { get; }

        public ICommand ExitApplicationCommand { get; }

        #endregion

        #region Command Implemtations

        private void SwitchHomePage()
        {
            CurrentApplicationPage = ApplicationPage.Home;
        }



    

        private void SwitchServerPage()
        {
            CurrentApplicationPage = ApplicationPage.Server;
        }

        private void SwitchSettingsPage()
        {
            CurrentApplicationPage = ApplicationPage.Settings;
        }

        private void SwitchTablePage()
        {
            CurrentApplicationPage = ApplicationPage.Table;
        }

        private void SwitchLoginPage()
        {
            CurrentApplicationPage = ApplicationPage.Login;
        }

        private void SwitchChartPage()
        {
            CurrentApplicationPage = ApplicationPage.Charts;
        }

        private void PopupExitDialog()
        {
            var popup = new PopupViewModel()
            {
                IsSpecialPopup = true, 
                OkCommand = new CloseDialogAttachedCommand(o=>true, () => { Application.Current.MainWindow?.Close(); }), OkButtonText = "确定",
                CancelCommand = new CloseDialogAttachedCommand(o=>true, () => {}), CancelButtonText = "取消",
                Content = "是否退出应用？"
            };
            Logger.EnqueuePopup(popup);
        }

        #endregion


        #region ctor

        public MainWindowViewModel()
        {
            SwitchHomePageCommand = new SimpleCommand(o => SwitchHomePage(),
                o => CurrentApplicationPage != ApplicationPage.Home);
            SwitchServerPageCommand = new SimpleCommand(o => SwitchServerPage(),
                o => CurrentApplicationPage != ApplicationPage.Server);
            SwitchSettingsPageCommand = new SimpleCommand(o => SwitchSettingsPage(),
                o => CurrentApplicationPage != ApplicationPage.Settings);
            SwitchTablePageCommand = new SimpleCommand(o => SwitchTablePage(),
                o => CurrentApplicationPage != ApplicationPage.Table);
            SwitchLoginPageCommand = new SimpleCommand(o => SwitchLoginPage(),
                o => CurrentApplicationPage != ApplicationPage.Login);
            SwitchChartPageCommand = new SimpleCommand(o => SwitchChartPage(),
                o => CurrentApplicationPage != ApplicationPage.Charts);
            OpenHelpDocsCommand = new RelayCommand(() =>
                Process.Start(Path.Combine(DirectoryConstants.DocumentaryDir, "IA906调试说明书.docx")));

            ExitApplicationCommand = new RelayCommand(PopupExitDialog);
        }

        #endregion
    }
}